package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.mapper.SystemDictionaryMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemDictionaryService implements ISystemDictionaryService {
    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;
    @Override
    public void saveOrUpdate(SystemDictionary systemDictionary) {
        if(systemDictionary.getId() != null){
            systemDictionaryMapper.updateByPrimaryKey(systemDictionary);
        } else {
            systemDictionaryMapper.insert(systemDictionary);
        }
    }

    @Override
    public void delete(Long id) {
        if(id != null){
            systemDictionaryMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public SystemDictionary getSystemDictionaryById(Long id) {
        return systemDictionaryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionary> getAll() {
        return systemDictionaryMapper.selectAll();
    }

    @Override
    public PageInfo<SystemDictionary> selectByQuery(QueryObject queryObject) {
//        int totalCount = systemDictionaryMapper.countByQuery(queryObject);
//        if(totalCount == 0){
//            return new PageResult(1,queryObject.getPageSize(),0, Collections.emptyList());
//        }
        //用pageInfo   调用方法
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.selectByQuery(queryObject);
        return new PageInfo<>(systemDictionarys);
    }
}
